﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FinanSoft.Entities;
using FinanSoft.BusinessLayer;
using FinanSoft.Utilities;

namespace FinanSoft.PresentacionInventario
{
    public partial class frmArticulos : Form
    {
        List<int> _storeIds;
        List<int> _articulosIds;
        int _selectedStoreId;
        
        /// <summary>
        /// Cosntructor del form
        /// </summary>
        public frmArticulos()
        {
            InitializeComponent();
            llenarComboBodegas();
        }

        /// <summary>
        /// Llena el combobox con las bodegas a consultar
        /// </summary>
        private void llenarComboBodegas()
        {
            cmbBodegas.Items.Clear();

            List<StoreRoom> stores = StoreRoomManager.GetInstance().GetStoreRooms();
            if (!StoreRoomManager.GetInstance().IsError)
            {
                _storeIds = new List<int>();

                for (int ele = 0; ele < stores.Count; ele++)
                {
                    _storeIds.Add(stores[ele].Id);
                    cmbBodegas.Items.Add(stores[ele].Name);
                }
            }
            else
            {
                MessageBox.Show(StoreRoomManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }

            cmbBodegas.SelectedIndex = 0;
        }
        
       
        private void addRow(Item articulo)
        {
            ItemStoreRoomInfo storeInfo = articulo.GetStoreRoomInfo(_selectedStoreId);
            _articulosIds.Add(articulo.Id);
            gridArticulos.Rows.Add(new object[] { articulo.CodArticulo, ItemManager.GetInstance().GetMeasurementUnit(articulo.UnidadMedida).acronimo, articulo.Descripción, storeInfo.Stock, storeInfo.Comprometido, storeInfo.Solicitado,
                storeInfo.Stock + storeInfo.Solicitado - storeInfo.Comprometido, MoneyManager.GetInstance().LocalCurrency.Symbol + " " + storeInfo.CostoUnitario.ToString("0.00") });
        }

        /// <summary>
        /// Llena el grid con los articulos que pertenecen a la bodega que se selecciono para consulta
        /// </summary>
        private void llenarGridArticulos()
        {
            gridArticulos.Rows.Clear();
            
            if (cmbBodegas.SelectedIndex == -1)
                MessageBox.Show(MessageConstants.NO_SELECTED_STORAGE_C, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                //Extrae el id de la bodega seleccionada
                int idBodega = _storeIds[cmbBodegas.SelectedIndex];

                List<Item> items = ItemManager.GetInstance().GetItems();
                if (!StoreRoomManager.GetInstance().IsError)
                {
                    _articulosIds = new List<int>();
                    _selectedStoreId = _storeIds[cmbBodegas.SelectedIndex];
                    foreach (Item articulo in items)
                        addRow(articulo);
                }
                else
                {
                    MessageBox.Show(StoreRoomManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Close();
                }                
            }
        }


        private void btnCrear_Click(object sender, EventArgs e)
        {
            frmCrearArticulo frmCrearArticuloV = new frmCrearArticulo();
            frmCrearArticuloV.ShowDialog(this);
            llenarGridArticulos();
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            if (gridArticulos.RowCount > 0)
            {
                //Obtener el objeto a editar
                Item articuloEdit = ItemManager.GetInstance().GetItem(_articulosIds[gridArticulos.CurrentRow.Index]);
                frmCrearArticulo frmCrearArticuloV = new frmCrearArticulo(articuloEdit, _selectedStoreId);
                frmCrearArticuloV.ShowDialog(this);
                llenarGridArticulos();
            }
            else
            {
                MessageBox.Show(MessageConstants.NO_SELECTED_ITEM, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }

        private void btnSalir_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnConsultar_Click(object sender, EventArgs e)
        {
            llenarGridArticulos();
        }

        private void btnBorrar_Click(object sender, EventArgs e)
        {
            if (gridArticulos.SelectedRows.Count > 0)
            {
                //Obtener el objeto a editar
                int idItem = _articulosIds[gridArticulos.CurrentRow.Index];

                ItemManager.GetInstance().DeleteItem(idItem);

                if (ItemManager.GetInstance().IsError)
                    MessageBox.Show(ItemManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    MessageBox.Show(MessageConstants.ITEM_DELETE, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    llenarGridArticulos();
                }
            }
            else
            {
                MessageBox.Show(MessageConstants.NO_SELECTED_ITEM, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

    }
}
